package com.hui.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.admin.common.enums.ErrorCodeEnum;
import com.hui.admin.common.exception.UserException;
import com.hui.admin.dto.request.CategoryPageRequestDTO;
import com.hui.admin.dto.request.CategorySaveRequestDTO;
import com.hui.admin.dto.response.CategoryListDTO;
import com.hui.admin.dto.response.CategoryPageResponseDTO;
import com.hui.admin.entity.CategoryDO;
import com.hui.admin.mapper.CategoryMapper;
import com.hui.admin.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 文章分类服务实现类
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryDO> implements CategoryService {
    @Override
    public CategoryPageResponseDTO getCategoryList(CategoryPageRequestDTO requestDTO) {
        Page<CategoryDO> page = new Page<>(requestDTO.getPageNum(), requestDTO.getPageSize());

        LambdaQueryWrapper<CategoryDO> queryWrapper = new LambdaQueryWrapper<>();

        if (requestDTO.getCategoryName() != null) {
            queryWrapper.like(CategoryDO::getCategoryName, requestDTO.getCategoryName());
        }

        // 按发布时间降序排列
        queryWrapper.orderByDesc(CategoryDO::getPublishTime);

        Page<CategoryDO> resultPage = baseMapper.selectPage(page, queryWrapper);

        List<CategoryListDTO> dtoList = resultPage.getRecords().stream().map(categoryDO -> {
            CategoryListDTO dto = new CategoryListDTO();
            dto.setId(categoryDO.getId());
            dto.setCategoryName(categoryDO.getCategoryName());
            dto.setPublishTime(categoryDO.getPublishTime());
            return dto;
        }).toList();

        CategoryPageResponseDTO result = new CategoryPageResponseDTO();
        result.setList(dtoList);
        result.setTotal(resultPage.getTotal());

        return result;
    }

    @Override
    public void createCategory(CategorySaveRequestDTO requestDTO) {
        CategoryDO categoryDO = BeanUtil.copyProperties(requestDTO, CategoryDO.class);
        boolean flag = save(categoryDO);
        if (!flag) {
            throw new UserException(ErrorCodeEnum.CATEGORY_ADD_FAILED);
        }
    }

    @Override
    public void deleteCategory(Long id) {
        LambdaQueryWrapper<CategoryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryDO::getId, id);
        CategoryDO result = getOne(queryWrapper);
        if (result == null) {
            throw new UserException(ErrorCodeEnum.CATEGORY_NOT_EXISTS);
        }
        boolean flag = removeById(id);
        if (!flag) {
            throw new UserException(ErrorCodeEnum.CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public void deleteArticles(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new UserException(ErrorCodeEnum.CATEGORY_NOT_EXISTS);
        }

        boolean flag = removeByIds(Arrays.asList(ids));

        if (!flag) {
            throw new UserException(ErrorCodeEnum.CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public void updateCategory(CategorySaveRequestDTO requestDTO) {
        LambdaQueryWrapper<CategoryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryDO::getId, requestDTO.getId());
        CategoryDO categoryDO = getOne(queryWrapper);
        if (categoryDO == null) {
            throw new UserException(ErrorCodeEnum.CATEGORY_NOT_EXISTS);
        }
        CategoryDO result = BeanUtil.copyProperties(requestDTO, CategoryDO.class);
        boolean flag = updateById(result);
        if (!flag) {
            throw new UserException(ErrorCodeEnum.CATEGORY_NOT_EXISTS);
        }
    }
}
